<?php
namespace app\index\controller;

use app\common\model\ClassifyModel;
use app\common\model\GoodsModel;
use think\Controller;
use think\Cookie;
use think\Session;

class Index extends Controller
{
    //获取用户名称
    protected function getUname(){
        $phone = Session::get('phone');
        $name = Session::get('name');
        $uname = empty($name)?$phone:$name;
        if (!empty($uname)){
            $this->assign([
                'uname'=>$uname
            ]);
        }
    }

    //判断是不是手机
    protected function isPhone(){
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $is_android = (strpos($agent, 'android')) ? true : false;
        $is_iphone = (strpos($agent, 'iphone')) ? true : false;
        if ($is_android || $is_iphone){
            return true;
        }else{
            return false;
        }
    }

    //首页页面
    public function index()
    {
        $this->getUname();
        if (Session::get('url')){
            $url = Session::get('url');
            Session::delete('url');
            $this->redirect($url['url'],['good_id'=>$url['good_id']]);
        }

        return $this->fetch();
    }

    //花椒页面
    public function huajiao()
    {
        $this->getUname();
        return $this->fetch();
    }

    //花椒价值页面
    public function huajiaovalue()
    {
        $this->getUname();

        return $this->fetch();
    }

    //辣椒页面
    public function lajiao()
    {
        $this->getUname();

        return $this->fetch();
    }

    //花椒产品页
    public function prodhuajiao()
    {
        $param = input("param.");
        $this->getUname();

        $classify = new ClassifyModel();
        $sons = $classify->getClassifySon(['clas_parentId'=>1]);
        $html = "";


        if (!empty($sons)){

            $classify = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];

            foreach ($sons as $k=>$son){
                $html .= '<div class="tab-huajiao"><a href="'.url('index/prodhuajiao').'?classify='.$son['clas_id'].'" id="data-classify'.$k.'"'.($classify==$son['clas_id']?' class="active"':'').'>'.$son['clas_name'].'</a></div>';
            }

            $where['good_classify'] = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];
        }else{
            //没有子分类不展示数据
            $where['good_classify'] = 0;

        }

        // 查询状态为1的用户数据 并且每页显示10条数据
        $goods = new GoodsModel();
        $where['good_classify'] = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];
        $where['good_delete'] = 0;
        $where['good_type'] = 1;
        $isPhone = $this->isPhone();

        if ($isPhone){
            $list = $goods->where($where)->order('good_id desc')->paginate(8,false, ['query'=>['classify'=>isset($param['classify'])?$param['classify']:$sons[0]['clas_id']]]);

        }else{
            $list = $goods->where($where)->order('good_id desc')->paginate(9,false, ['query'=>['classify'=>isset($param['classify'])?$param['classify']:$sons[0]['clas_id']]]);

        }


        $this->assign([
            'list'=>$list,
            'html'=>$html
        ]);


        return $this->fetch();
    }

    //辣椒产品页
    public function prodlajiao()
    {
        $this->getUname();
        $param = input("param.");
        $this->getUname();

        $classify = new ClassifyModel();
        $sons = $classify->getClassifySon(['clas_parentId'=>2]);
        $html = "";

        if (!empty($sons)){

            $classify = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];

            foreach ($sons as $k=>$son){
                $html .= '<div class="tab-lajiao'.($classify==$son['clas_id']?' active':'').'"><a href="'.url('index/prodlajiao').'?classify='.$son['clas_id'].'">'.$son['clas_name'].' <i class="iconfont arrow-right"></i></a></div>';
            }

            $where['good_classify'] = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];
        }else{
            //没有子分类不展示数据
            $where['good_classify'] = 0;

        }



        // 查询状态为1的用户数据 并且每页显示10条数据
        $where['good_delete'] = 0;
        $where['good_type'] = 2;
        $goods = new GoodsModel();

        $isPhone = $this->isPhone();

        if ($isPhone){
            $list = $goods->where($where)->order('good_id desc')->paginate(8,false, ['query'=>['classify'=>isset($param['classify'])?$param['classify']:$sons[0]['clas_id']]]);

        }else{
            $list = $goods->where($where)->order('good_id desc')->paginate(9,false, ['query'=>['classify'=>isset($param['classify'])?$param['classify']:$sons[0]['clas_id']]]);

        }


        $this->assign([
            'list'=>$list,
            'html'=>$html
        ]);
        return $this->fetch();
    }

    //香料产品页
    public function prodxiangliao()
    {
        $param = input("param.");
        $this->getUname();

        $classify = new ClassifyModel();
        $sons = $classify->getClassifySon(['clas_parentId'=>3]);
        $html = "";

        if (!empty($sons)){

            $classify = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];

            foreach ($sons as $k=>$son){
                $html .= '<div class="tab-lajiao'.($classify==$son['clas_id']?' active':'').'"><a href="'.url('index/prodxiangliao').'?classify='.$son['clas_id'].'">'.$son['clas_name'].' <i class="iconfont arrow-right"></i></a></div>';
            }

            $where['good_classify'] = isset($param['classify'])?$param['classify']:$sons[0]['clas_id'];
        }else{
            //没有子分类不展示数据
            $where['good_classify'] = 0;

        }



        // 查询状态为1的用户数据 并且每页显示10条数据
        $where['good_delete'] = 0;
        $where['good_type'] = 3;
        $goods = new GoodsModel();

        $isPhone = $this->isPhone();

        if ($isPhone){
            $list = $goods->where($where)->order('good_id desc')->paginate(8,false, ['query'=>['classify'=>isset($param['classify'])?$param['classify']:$sons[0]['clas_id']]]);

        }else{
            $list = $goods->where($where)->order('good_id desc')->paginate(9,false, ['query'=>['classify'=>isset($param['classify'])?$param['classify']:$sons[0]['clas_id']]]);

        }


        $this->assign([
            'list'=>$list,
            'html'=>$html
        ]);
        return $this->fetch();
    }

    //香料页面
    public function xiangliao()
    {
        $this->getUname();

        return $this->fetch();
    }

    //产品详情页
    public function proddetails()
    {
        $this->getUname();
        $param = input('param.');
        $goods = new GoodsModel();
        $row = $goods->getOneGoods($param['good_id']);
        Cookie::set('good_id',$param['good_id'],3600*24);//cookie保存商品id
        //点击数+1
        $goods->where(['bran_id'=>$param['good_id']])->setInc('goods_hits', 1);

        //保存浏览记录
        if(Cookie::get('his')){
            $ids = $_COOKIE['his'];//读取cookie
            $arr = unserialize($ids);//字符串转回原来的数组
            $arr[] = $param['good_id'];//当前页面url添加到数组中
            $arr = array_unique($arr);//除去重复的
            if(count($arr)>3){//只保存3条访问记录
                array_shift($arr);
            }
            $ids = serialize($arr);//存储为字符串，
            Cookie::set('his',$ids);//保存到cookie当中
        }else{
            $url = $param['good_id'];//获取当前页面URL
            $arr[] = $url;//将当前URL保存到数组中
            $ids = serialize($arr);//存储为字符串
            Cookie::set('his',$ids);//保存到cookie当中
        }
        $this->assign([
            'goods'=>$row
        ]);

        return $this->fetch();
    }

    //注册页面
    public function register()
    {
        return $this->fetch();
    }

    //登陆页面
    public function login(){

        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true && !isset($_SESSION['openid'])){
            $wechat = new WeChat();
            $arr = $wechat->getUser();
            Session::set('openid',$arr['openid']);

        }
        $token = md5(md5(rand(1,999999)));
        Session::set('token',$token);
        return $this->fetch();
    }

    //修改密码页面
    public function user_pwd_update()
    {
        $this->getUname();
        $userinfo = isset($_SESSION['id'])?true:false;

        $this->assign([
            'userinfo'=>$userinfo
        ]);

        return $this->fetch();
    }

    //精品展示
    public function prodquality()
    {
//        $param = input("param.");
        $this->getUname();

        // 查询状态为1的用户数据 并且每页显示10条数据
        $where['good_delete'] = 0;
        $where['good_type'] = 4;
        $where['good_classify'] = 18;
        $goods = new GoodsModel();

        $isPhone = $this->isPhone();

        if ($isPhone){
            $list = $goods->where($where)->order('good_id desc')->paginate(8);

        }else{
            $list = $goods->where($where)->order('good_id desc')->paginate(9);

        }


        $this->assign([
            'list'=>$list
        ]);
        return $this->fetch();
    }

    //最新资讯
    public function news()
    {
        return $this->fetch();
    }

    //故事欣赏
    public function story()
    {
        return $this->fetch();
    }
}
